package proc

import (
	"github.com/PuerkitoBio/goquery"
	"github.com/bitly/go-simplejson"
	"github.com/hu17889/go_spider/core/common/page"
	corerequest "github.com/hu17889/go_spider/core/common/request"
	"github.com/hu17889/go_spider/core/pipeline"
	"github.com/hu17889/go_spider/core/spider"
	"net/http"
	"strings"
	"tesou.io/platform/poem-parent/poem-api/common/base"
	"tesou.io/platform/poem-parent/poem-api/module/core/enums"
	"tesou.io/platform/poem-parent/poem-api/module/core/pojo"
	"tesou.io/platform/poem-parent/poem-core/module/core/service"
)

type BaiduProcesser struct {
	BaiduService    service.BaiduService
	SpiderUrl string
	cookies []*http.Cookie
}

func GetBaiduProcesser() *BaiduProcesser {
	return &BaiduProcesser{}
}

func (this *BaiduProcesser) Startup() {
	newSpider := spider.NewSpider(this, "BaiduProcesser")

	//newSpider = newSpider.AddUrl(this.SpiderUrl, "html")
	base.Log.Info("start URL:,", this.SpiderUrl)
	hder := make(http.Header)
	hder.Add("User-Agent", `Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Mobile Safari/537.36`)
	hder.Add("Upgrade-Insecure-Requests", `1`)
	hder.Add("Host", `top.baidu.com`)

	req := corerequest.NewRequest(this.SpiderUrl, "html", "jandanindex", "GET", "", hder, nil, nil,nil)
	newSpider.AddRequest(req)

	newSpider = newSpider.AddPipeline(pipeline.NewPipelineConsole())
	newSpider.SetThreadnum(1).Run()
}

func (this *BaiduProcesser) Process(p *page.Page) {
	request := p.GetRequest()
	if !p.IsSucc() {
		base.Log.Info("URL:,", request.Url, p.Errormsg())
		return
	}
	//var allData []map[string]interface{}
	zhi_list_slice := make([]interface{}, 0)
	zhi_list_update_slice := make([]interface{}, 0)
	level := enums.GetListLevel("百度")
	p.GetHtmlParser().Find("body").Each(func(i int, selection *goquery.Selection) {
		aaa,_:= selection.Html()

		aaaword := strings.Split(aaa, "s-data:")[1]  //, "guestFeeds"
		bbbword := strings.Split(aaaword, "-->")[0]
		bbbword = strings.TrimSpace(bbbword)

		js, err2 := simplejson.NewJson([]byte(bbbword))
		if err2 != nil {
			base.Log.Info("err2:,", err2)
		}
		test2,err2 := js.Get("selectedTab").Int()
		if err2 !=nil{
			base.Log.Info("test:,", err2)
		}
		base.Log.Info("test:,", test2)
		for i < 25 {
			test := js.Get("cards").GetIndex(0).Get("content").GetIndex(i).MustMap()
			base.Log.Info("test:,", test["word"])
			if test["word"] != nil  {
				zhi := new(pojo.List)
				zhi.ListId = ""
				zhi.Title = strings.TrimSpace(test["word"].(string))
				zhi.SUrl = strings.TrimSpace(test["url"].(string))
				img_temp := test["img"]
				if img_temp != nil {
					zhi.Image = strings.TrimSpace(img_temp.(string))
				}
				desc_temp := test["desc"]
				if desc_temp != nil {
					zhi.Content = strings.TrimSpace(desc_temp.(string))
				}

				if len(zhi.Title) > 0 {
					//设置朝代
					zhi.Type = level
					exist := this.BaiduService.Exist(zhi)
					if !exist {
						zhi_list_slice = append(zhi_list_slice, zhi)
					} else {
						zhi_list_update_slice = append(zhi_list_update_slice, zhi)
					}
				}
			}
			i++
		}

	})
	//执行入库
	this.BaiduService.SaveList(zhi_list_slice)
	this.BaiduService.ModifyList(zhi_list_update_slice)

}

func (this *BaiduProcesser) Finish() {
	base.Log.Info("zhihu表抓取解析完成 \r\n")

}

